// 分类模块
import { topCategory } from '../../api/constains'
import { findAllCategory } from '../../api/category'
export default {
  namespaced: true,
  state: () => {
    return {
      // 分类依赖于 api下constains中的topCategory    现在是对象 了  {name: '美食'}
      list: topCategory.map(item => ({ name: item }))
    }
  },
  // 修改分类函数
  mutations: {
    setList (state, payload) {
      state.list = payload
    },
    // 定义类目的显示隐藏
    show (state, id) {
      const currentCategory = state.list.find(item => item.id === id)
      currentCategory.open = true
    },
    hide (state, id) {
      const currentCategory = state.list.find(item => item.id === id)
      currentCategory.open = false
    }
  },
  // 获取分类函数
  actions: {
    async getList ({ commit }) {
      const data = await findAllCategory()
      // 给每个一级类目加上open
      data.result.forEach(item => {
        item.open = false
      })
      // console.log(data)
      commit('setList', data.result)
    }
  }
}
